﻿using System.Collections.Generic;
using UnityEngine;

namespace Core
{
    public class Utils
    {
        public static void GetUnitByRangeXZ(Unit unit, float range, List<Unit> resultList)
        {
            var colliders = Physics.OverlapSphere(unit.transform.position, range);
            var unitComponent = World.GetComponent<UnitComponent>();
            foreach (var collider in colliders)
            {
                if(unitComponent.TryGetUnit(collider.transform, out var collideUnit))
                {
                    if (collideUnit == unit)
                        continue;
                    resultList.Add(collideUnit);
                }
            }
        }

        public static void GetUnitBSector(Unit unit, int range, int angle, List<Unit> resultList)
        {
            var colliders = Physics.OverlapSphere(unit.transform.position, range);
            var unitComponent = World.GetComponent<UnitComponent>();
            foreach (var collider in colliders)
            {
                if (unitComponent.TryGetUnit(collider.transform, out var collideUnit))
                {
                    if (collideUnit == unit)
                        continue;
                    var _angle = Vector3.Angle(unit.transform.forward, collider.transform.position - unit.transform.position);
                    if (_angle <= angle)
                    {
                        resultList.Add(collideUnit);
                    }
                }
            }
        }
    }
}